//
//  FindViewController.swift
//  OverseasOnlineShopping
//
//  Created by 唐先才 on 16/3/11.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import UIKit

class FindViewController: BaseCollectionViewController {

    var page: Int = 0
    var isPulling: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        page = 1
        createData()
        createRefresh()
    }

    func createData() {
        if isPulling {
            self.baseDataArray.removeAllObjects()
        }
        
        let manager = AFHTTPRequestOperationManager()
        let parameters = ["brightUrl": "",
                          "catUrl": "",
                          "exceptMallUrl": "",
                          "homeShow": "0",
                          "infoNation": "0",
                          "infoType": "2",
                          "isTop": "1",
                          "keyWord": "",
                          "mallUrl": "",
                          "pageIndex": page,
                          "pageSize": "20",
                          "tagUrl": "",
                         ]
        manager.POST(infomation_url, parameters: parameters, success: { (operation: AFHTTPRequestOperation!, responsObject: AnyObject!) -> Void in
            guard let array = responsObject as? [[String: AnyObject]] else {
                return
            }
            for dict in array {
                let model = TxcModel(dict: dict)
                self.baseDataArray.addObject(model)
            }
            self.baseCollectionView.reloadData()
            }) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
            SVProgressHUD.showErrorWithStatus(AppNetErrorTips)
        }
    }
    
    func createRefresh() {
        self.baseCollectionView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: "downRefresh")
        self.baseCollectionView.mj_footer = MJRefreshAutoFooter(refreshingTarget: self, refreshingAction: "upRefresh")
    }
    
    func downRefresh() {
        isPulling = true
        self.page = 1
        self.baseCollectionView.mj_header.beginRefreshing()
        self.createData()
        self.baseCollectionView.mj_header.endRefreshing()
    }
    
    func upRefresh() {
        isPulling = false
        page++
        self.baseCollectionView.mj_footer.beginRefreshing()
        self.createData()
        self.baseCollectionView.mj_footer.endRefreshing()
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
